﻿using Common;
using CommonModel;
using Domain.Database;
using IBll;
using MainService.Controllers.Extend;
using ResponseModel;
using System;
using System.Web.Http;

namespace MainService.Controllers
{
    /// <summary>
    /// 配置控制器
    /// </summary>
    public class ConfigController : BaseApiController
    {
        public IConfigBll ConfigBll { get; set; }

        /// <summary>
        /// 修改配置
        /// </summary>
        /// <param name="configRequest"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel UpdateConfig([FromUri] ConfigRequest configRequest)
        {
            ResultModel resultModel = new ResultModel();
            var obj = RequestBase["Key"];

            try
            {
                LogHelper.Debug("入参：" + configRequest.ToJson());
                if (ConfigBll.UpdateConfig(configRequest))
                {
                    resultModel.Pass = true;
                    resultModel.Msg = "修改配置成功";
                }
                else
                {
                    resultModel.Msg = "修改配置失败";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("报错：" + ex);
                resultModel.Msg = "报错：" + ex;
            }

            return resultModel;
        }

        /// <summary>
        /// 查询配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel SearchConfig(string key)
        {
            ResultModel resultModel = new ResultModel();

            try
            {
                Config config = ConfigBll.SearchConfig(key);
                if (config != null)
                {
                    resultModel.Pass = true;
                    resultModel.Msg = "查询配置成功";
                    resultModel.Append = config;
                }
                else
                {
                    resultModel.Msg = "查询配置失败";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("报错：" + ex);
                resultModel.Msg = "报错：" + ex;
            }

            return resultModel;
        }

        /// <summary>
        /// 删除配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel DeleteConfig(string key)
        {
            ResultModel resultModel = new ResultModel();

            try
            {
                if (ConfigBll.DeleteConfig(key))
                {
                    resultModel.Pass = true;
                    resultModel.Msg = "删除配置成功";
                }
                else
                {
                    resultModel.Msg = "删除配置失败";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("报错：" + ex);
                resultModel.Msg = "报错：" + ex;
            }

            return resultModel;
        }
    }
}
